package biolabs.model.division

import biolabs.core.Genomic
import biolabs.core.Instantiable
import biolabs.space.ContinuousPosition
import biolabs.space.DiscretePosition
import biolabs.space.GridShape
import biolabs.space.GridShapedObject

object DivisibleByVolume {
	trait Genome {
		val divisionThreshold: Double
	}
}

trait DivisibleByVolume[
	Self <: DivisibleByVolume[Self, P, C],
	P <: DiscretePosition[P, C],
	C <: ContinuousPosition[C, P]
]
extends Divisible[Self, P]
with GridShapedObject[P, C]
with Instantiable[Self]
{

self:
	Self
	with SpaceDivider[P]
	with Genomic[DivisibleByVolume.Genome]
=>

	override def divisionDelegate(): Option[DivisionDelegate[P, Self]] = {
		if(getShape().getVolume() < genome.divisionThreshold)
			return None
		val space = calculateDaughterSpace()
		return Some(new MyDivisionDelegate(space))
	}

	def handleDivision(dd: DivisionDelegate[P, Self]) {
		getShape().removeElements(dd.space)
	}

	private class MyDivisionDelegate(space: java.util.List[P]) extends DivisionDelegate[P, Self](space) {

		private lazy val newCell: Self = newInstance()

		override def daughter(): Self = {
			newCell.setShape(new GridShape[P,C](space))
			return newCell
		}

	}

}